package svg.sample;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas;

public class Test extends JDialog {

    private JSVGCanvas canvas;

    private JButton btnOK;
    private JButton btnCancel;

    private int x1, y1, x, y;
    private boolean mouseButtonDown = false;

    public Test(JFrame owner, String title) {
        super(owner, title, true);

        canvas = new JSVGCanvas();

        //add mouse (e.g press, release) event handler
        canvas.addMouseListener(new MouseHandler());

        //add mouse motion (e.g mouse drag) event handler
        canvas.addMouseMotionListener(new MouseMotionHandler());

        // specify the size of the dialog
        setSize(400, 300);

        // add the components to the container
        Container contentPane = getContentPane();
        contentPane.add(canvas, BorderLayout.CENTER);
        contentPane.add(createSouthPanel(), BorderLayout.SOUTH);
    }

    private JPanel createSouthPanel() {
        JPanel panel = new JPanel(new FlowLayout());

        btnOK = new JButton("OK");
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        panel.add(btnOK);
        panel.add(btnCancel);

        return panel;
    }

    //MouseEventListeners: for creating points--added
    
    class MouseHandler implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            //this is how u get x and y coordinates...simple and sweet
            //System.out.println(e.getX() + ":" + e.getY());
        }

        public void mouseEntered(MouseEvent e) {}

        public void mouseExited(MouseEvent e) {}

        public void mousePressed(MouseEvent e) {
            x1 = e.getX();
            y1 = e.getY();
            mouseButtonDown = true;
        }

        public void mouseReleased(MouseEvent e) {
            mouseButtonDown = false;
        }
    }

    class MouseMotionHandler implements MouseMotionListener {
        public void mouseDragged(MouseEvent e) {
            if (mouseButtonDown) {
                Graphics2D g = (Graphics2D)canvas.getGraphics();
                x = e.getX();
                y = e.getY();
                //make the line have thickness and the color red
                Stroke stroke = new BasicStroke(2.0f);
                g.setColor(Color.red);
                g.setStroke(stroke);
                g.drawLine(x1, y1, x, y);
                x1 = x;
                y1 = y;
            }
        }

        public void mouseMoved(MouseEvent e) {}
    }

}
